<?php

namespace App\Traits;


use App\Common\SysContext;
use App\Common\WX\Wx;
use App\Model\t_s_accsuper_config;
use XWX\Common\H;
use XWX\Common\XReturn;


//将NOTICE错误改了异常错误
error_reporting(E_NOTICE);

set_error_handler(function ($errno, $errstr, $errfile, $errline)
{
    if (error_reporting() & $errno)
    {
        throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
    }
});


trait TraitCommon
{
    /**
     * 协程上下文
     *
     * @return SysContext
     */
    protected function context()
    {
        return SysContext::funcInsStatic();
    }

    /**
     * 返回
     *
     * @param $errcode
     * @param $errmsg
     * @param null $data
     * @return XReturn
     */
    public function funcGetR($errcode, $errmsg = '', $data = null)
    {
        $r = new XReturn();

        $r->errcode = $errcode;
        $r->errmsg = $errmsg;
        $r->setVal('return_data', $data);

        return $r;
    }


    /**
     * get X
     *
     * @return string
     * @throws \Exception
     */
    public function funcGetX()
    {
        $x = $this->context()->funcGetX();
        if (H::funcStrIsNullOrEmpty($x))
        {
            $x = $this->reqParam('x');

            $this->funcSetX($x);
        }


        return $x;
    }


    /**
     * get config
     *
     * @return t_s_accsuper_config|null
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function funcGetX__config()
    {
        $entity = $this->context()->funcGetX__config();


        return $entity;
    }


    /**
     * @return Wx
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function funcGetAppWx()
    {
        $app = Wx::funcIns($this->funcGetX__config());

        return $app;
    }

    /**
     * set X
     * @param $x
     */
    public function funcSetX($x)
    {
        $this->context()->funcSetX($x);
    }

}